import { TargetObjectFilter, TargetObjectType, TargetObject } from './targetObject.js';
import { Engine } from '../engine.js';
import { Util } from '../../utils/util.js';

export class SupplierFilter extends TargetObjectFilter {
    constructor() {
        super();
    }

    static clone(filter) {

        let baseFilter = TargetObjectFilter.clone(filter);

        let instance = new SupplierFilter();
        Object.assign(instance, baseFilter);

        return instance;
    }

    toJson() {
        return super.toJson();
    }
}

export class Supplier extends TargetObject {
    constructor({
        id = '',
        name = '',
        contactName = '',
        contactPhoneNumber = '',
        email = '',
        city = '',
        address = '',
        remark = ''
    } = {}) {
        super({
            id: id,
            name: name,
            contactName: contactName,
            contactPhoneNumber: contactPhoneNumber,
            email: email,
            city: city,
            address: address,
            remark: remark,
            objType: TargetObjectType.TSupplier
        });
    }

    static clone(supplier) {

        let baseObj = TargetObject.clone(supplier);

        let instance = new Supplier();
        Object.assign(instance, baseObj);
        return instance;
    }

    static cloneFromTargetObject(targetObj) {
        let baseObj = TargetObject.clone(targetObj);

        let instance = new Supplier();
        Object.assign(instance, baseObj);
        return instance;
    }

    static empty() {
        return new Supplier();
    }

    static fromJson(json) {
        let baseObj = TargetObject.fromJson(json);
        let instance = new Supplier();
        Object.assign(instance, baseObj);
        return instance;
    }

    toJson() {
        return {
            id: this.id,
            code: this.code || '',
            orgId: Engine.profile?.org?.orgId,
            name: this.name,
            contactUser: this.contactName,
            contactPhone: this.contactPhoneNumber,
            contactEmail: this.email,
            city: this.city,
            detailAddr: this.address,
            remark: this.remark,
            customerType: 1,   // 0 for customer, 1 for supplier
             custState: this.enabled ? 1:0,
            ...(Util.isEmpty(this.id) && { ign_init_balance: this.initFundValue?.value || 0 })
        };
    }

    toCopyJson() {
        return super.toCopyJson();
    }
}